package epam.marchenko.library.services;

import java.util.ArrayList;
import java.util.Collection;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.GrantedAuthorityImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import epam.marchenko.library.domain.User;

@Service("assembler")
public class Assembler {

	@Transactional(readOnly = true)
	org.springframework.security.core.userdetails.User buildUserFromUserEntity(User userEntity) {
		String username = userEntity.getLogin();
		String password = userEntity.getPassword();
		Collection<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
		authorities.add(new GrantedAuthorityImpl("ROLE_USER"));
		if (username.equals("admin")) authorities.add(new GrantedAuthorityImpl("ROLE_ADMIN"));
		return new org.springframework.security.core.userdetails.User(username, password, true, true, true, true, authorities);
	}
}
